//
//  SDHSportsRatioResultView.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/11.
//

#import "SDHSportsRatioResultView.h"

#define kViewBaseTag 1001
const static CGFloat kWinRateDescWidth = 16;

@implementation SDHSportsRatioResultView

- (id)init {
    if (self = [super init]) {
        UILabel *label = nil;
        UIView *lastView = self;
        __weak typeof(self) weakSelf = self;
        for (int i = 0; i < 6; i++) {
            label = [UILabel new];
            label.text = @"胜";
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:10];
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor redColor];
            label.layer.cornerRadius = kWinRateDescWidth/2;
            label.clipsToBounds = YES;
            label.tag = kViewBaseTag+i;
            [self addSubview:label];
            
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                if (i == 0) {
                    make.left.equalTo(lastView).offset(0);
                } else {
                    make.left.equalTo(lastView.mas_right).offset(5);
                }
                make.top.equalTo(weakSelf);
                make.width.equalTo(@(kWinRateDescWidth));
                make.height.equalTo(@(kWinRateDescWidth));
            }];
            
            lastView = label;
        }
        
        CGFloat width = (kWinRateDescWidth + 5) * 6 - 5;
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(kWinRateDescWidth));
            make.width.equalTo(@(width));
        }];
    }
    
    return self;
}

- (void)updateContent:(NSArray *)array {
    NSString *result = nil;
    UILabel *label = nil;
    for (int i = 0; i < 6; i++) {
        label = [self viewWithTag:kViewBaseTag + i];
        if (!label) {
            continue;
        }
        if (array.count > i) {
            result = array[i];
        }
        if (result.length > 0) {
            label.text = result;
            if ([result isEqual:@"胜"]) {
                label.backgroundColor = UIColorFromRGB(255, 34, 43);
            } else if ([result isEqual:@"负"]) {
                label.backgroundColor = UIColorFromRGB(84, 216, 133);
            } else if ([result isEqual:@"平"]) {
                label.backgroundColor = UIColorFromRGB(255, 219, 37);
            } else {
                label.text = @"";
                label.backgroundColor = [UIColor grayColor];
            }
        }
    }
}

@end
